package com.sgb.kjwl.view.ui.fragment.packers;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentTeamListBinding;
import com.sgb.kjwl.model.entity.ShopTeamInfoEntity;
import com.sgb.kjwl.view.ui.adapter.packers.TeamPerformanceAdapter;
import com.sgb.kjwl.view.ui.adapter.price.QuotePriceListAdapter;
import com.sgb.kjwl.viewmodel.packers.TeamServiceModel;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.dialog.DialogHelper;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @author: 王彬臣
 * @date: 2021/9/6
 * @Description: 企业网站-工程业绩
 */
public class PerformanceFragment extends AppLazyFragment {
    FragmentTeamListBinding mBinding;
    TeamServiceModel mViewModel;
    private int mPage;
    String websiteNo;
    List<ShopTeamInfoEntity> mList=new ArrayList<>();
    TeamPerformanceAdapter mAdapter;

    public static PerformanceFragment newInstance(String websiteNo){
        PerformanceFragment performanceFragment=new PerformanceFragment();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        performanceFragment.setArguments(args);
        return performanceFragment;
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_team_list, container, false);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
        }
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mViewModel=new ViewModelProvider(this).get(TeamServiceModel.class);
        mBinding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.refreshLayout.setEnableRefresh(false);
        mBinding.refreshLayout.setEnableLoadMore(false);
        mAdapter = new TeamPerformanceAdapter(null);
        mBinding.recycler.setAdapter(mAdapter);

        mViewModel.loadingLiveData.observe(getViewLifecycleOwner(), aBoolean -> DialogHelper.showProgressDialog(getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false));
        mViewModel.successLiveData.observe(getViewLifecycleOwner(), aBoolean -> DialogHelper.dismissProgressDialog());
        mViewModel.errorLiveData.observe(getViewLifecycleOwner(), aBoolean -> DialogHelper.dismissProgressDialog());

        mViewModel.ldPerformance.observe(getViewLifecycleOwner(), shopTeamInfoEntities -> {
            if (shopTeamInfoEntities.size() > 0){
                mAdapter.setNewData(shopTeamInfoEntities);
                mBinding.recycler.setVisibility(View.VISIBLE);
                mBinding.errorView.setVisibility(View.GONE);
                mBinding.tvNumber.setText(String.format(Locale.CHINA, "工程业绩%d项",shopTeamInfoEntities.size()));
                mBinding.tvNumber.setVisibility(View.VISIBLE);
            }else {
                mBinding.recycler.setVisibility(View.GONE);
                mBinding.errorView.setVisibility(View.VISIBLE);
                mBinding.tvNumber.setVisibility(View.GONE);
            }
        });

        mViewModel.getPerformance(websiteNo);
    }

    @Override
    protected void onLazyLoad() {
        mViewModel.getPerformance(websiteNo);
    }
}
